/**
 * @param {string} s
 * @param {number} k
 * @return {string}
 */
var reverseStr = function(s, k) {

    let res = ''
    let len = s.length
    for (let i = 0; i< len ; i += 2 * k) {
        // 如果剩余字符少于k 个 则将剩余的字符全部反转
        if (len - i < k) {
            res += reverse(s.slice(i))
            break;
        }
        // 如果剩余的字符小于2K 但大于或等于K个
        if (len - i < 2 *k && len - i >= k) {
            res +=  reverse(s.slice(i, i + k)) + s.slice(i +k)
            break;
        }
        res += reverse(s.slice(i, i+ k)) + s.slice(i +k, i+ 2 *k)

    }
    // 反转的函数
    function reverse(str) {
        let temp = ''
        for (let i = str.length- 1; i>=0; i--) {
            temp += str[i]
        }
        return temp
    }
    return res
};
/*var reverseStr = function(s, k) {

    let len = s.length
    let arr = Array.from(s)

    for (let i = 0; i< s.length; i++) {
        reverse(arr, i , Math.min(i +k, len) -1)
    }
    return arr.join('')
    function reverse(arr,left, right) {
        while (left < right) {
            let temp = arr[right]
            arr[right] = arr[left]
            arr[left] = temp
            left++
            right--
        }

    }
};*/
let s = "abcdefg"
let k = 2
console.log(reverseStr(s, k))